﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using KingRoad.EPM.InvestmeIBLL;
//using KingRoad.EPM.InvestmeEntity;
using KingRoad.EPM.Web.Util;
//using KingRoad.EPM.InvestmeBLL;
using KingRoad.EPM.ProjectInfoIBLL;
using KingRoad.EPM.ProjectInfoBLL;
using KingRoad.EPM.ProjectInfoEntity;
using DotNet.Model.MyEntity;

namespace KingRoad.EPM.Web.Areas.Permission.Controllers
{
    public class JLOperatorSetController : Controller
    {
        //
        // GET: /Permission/JLOperatorSet/
        private IProject_HTDWFRelagionBLL _HTDWFRelagionBLL = null;
        private IProject_SegmentBLL _SegmentBLL = null;
        private IProject_PrjInfoBLL prjInfoBLL = null;
        public JLOperatorSetController()
        {
            _HTDWFRelagionBLL = new Project_HTDWFRelagionBLL();
            _SegmentBLL = new KingRoad.EPM.ProjectInfoBLL.Project_SegmentBLL();
            prjInfoBLL = new Project_PrjInfoBLL();
        }
        public ActionResult Index(int ProjID = 1)
        {
            List<PROJECT_PRJINFO> PrjList = new List<PROJECT_PRJINFO>();
            PrjList = prjInfoBLL.GetAllPrjinfo();
            ViewBag.ProjList = PrjList;
            List<HTType> HTlist = new List<HTType>()
                {
                    new HTType(){ Flag="1", Name="施工标"},
                    new HTType(){ Flag="2", Name="监理标"},
                    //new HTType(){ Flag="3", Name="设计标"},
                    new HTType(){ Flag="4", Name="检测标"},
                    new HTType(){ Flag="5", Name="驻地监理标"}
                };
            ViewBag.JLOperatorSet_HTTypeList = HTlist;
            List<PROJECT_SEGMENT> list = new List<PROJECT_SEGMENT>();
            list = _SegmentBLL.GetlistByPrjinfoID(Convert.ToInt32(HTlist[0].Flag), ProjID);
            ViewBag.SegmentID = 0;
            ViewBag.SegmentCode = "";
            if (list != null && list.Count > 0)
            {
                ViewBag.SegmentID = list[0].SEGMENT_ID;
                ViewBag.SegmentCode = list[0].SEGMENT_CODE;
            }
            return View();
        }
        /// <summary>
        /// 获取所有标段
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllSegment(int id, int projID)
        {
            List<PROJECT_SEGMENT> list = new List<PROJECT_SEGMENT>();
            list = _SegmentBLL.GetlistByPrjinfoID(id, projID);
            return Json(list);
        }
        /// <summary>
        /// 根据标段ID获取数据列表
        /// </summary>
        /// <param name="SegmentID"></param>
        /// <returns></returns>
        public ActionResult GetlistBySegmentID(int SegmentID, int type, QueryBase query)
        {
            List<KingRoad.EPM.Entity.Base_Staff> list = new List<KingRoad.EPM.Entity.Base_Staff>();
            string outMessage = string.Empty;
            if (SegmentID > 0)
                list = _HTDWFRelagionBLL.GetlistBySegmentID(SegmentID, type, out outMessage);
            var jqGrodData = new jqGridQueryData<KingRoad.EPM.Entity.Base_Staff>();
            if (list != null)
                jqGrodData = WebUtility.TojqGridData<KingRoad.EPM.Entity.Base_Staff>(list, query);
            return Json(jqGrodData);
        }
        /// <summary>
        /// 验证数据
        /// </summary>
        /// <param name="SegmentID"></param>
        /// <returns></returns>
        public ActionResult CheckData(int SegmentID, int type)
        {
            string outMessage = string.Empty;
            string statuCode = "300";
            List<KingRoad.EPM.Entity.Base_Staff> list = null;
            if (SegmentID > 0)
            {
                list = new List<KingRoad.EPM.Entity.Base_Staff>();
                list = _HTDWFRelagionBLL.GetlistBySegmentID(SegmentID, type, out outMessage);
                if (list != null && list.Count > 0)
                {
                    statuCode = "200";
                }
            }
            return Json(new KingRoad.EPM.Web.ViewModels.CallbackStatus
            {
                message = outMessage,
                statusCode = statuCode,
                data = list,
                callbackType = "CheckData"
            });
        }
        /// <summary>
        /// 添加配置关系
        /// </summary>
        /// <param name="SegmentID"></param>
        /// <param name="SegmentCode"></param>
        /// <param name="StaffIDs"></param>
        /// <param name="DutyIDs"></param>
        /// <param name="UserNames"></param>
        /// <returns></returns>
        public ActionResult CreateJLOperatorSet(int SegmentID, int type, string SegmentCode, string StaffIDs, string DutyIDs)
        {
            string OutMessage = "保存失败，请重试！";
            string OutStatus = "300";
            List<PROJECT_HTDWFRELATION> list = null;
            if (SegmentID > 0)
            {
                if (!string.IsNullOrEmpty(StaffIDs) && !string.IsNullOrEmpty(DutyIDs))
                {
                    list = new List<PROJECT_HTDWFRELATION>();
                    string[] StaffID = StaffIDs.Remove(StaffIDs.Length - 1, 1).Split(',');
                    string[] DutyID = DutyIDs.Remove(DutyIDs.Length - 1, 1).Split(',');
                    if (StaffID.Length > 0 && DutyID.Length > 0)
                    {
                        PROJECT_HTDWFRELATION Entity = null;
                        for (int i = 0; i < StaffID.Length; i++)
                        {
                            Entity = new PROJECT_HTDWFRELATION();
                            if (!string.IsNullOrEmpty(DutyID[i]))
                                Entity.HTDWFRELATION_DUTYID = Convert.ToInt32(DutyID[i]);
                            else
                                continue;
                            if (!string.IsNullOrEmpty(StaffID[i]))
                            {
                                Entity.HTDWFRELATION_STAFFID = Convert.ToInt32(StaffID[i]);
                                Entity.HTDWFRELATION_USERNAME = (new DotNet.Service.UserService().GetUserEntityByID(Entity.HTDWFRELATION_STAFFID ?? 0) ?? new Base_User()).RealName;
                            }
                            else
                                continue;
                            Entity.HTDWFRELATION_SEGMENTID = SegmentID;
                            if (!string.IsNullOrEmpty(SegmentCode))
                                Entity.HTDWFRELATION_SEGMENTCODE = SegmentCode;
                            else
                                continue;
                            Entity.HTDWFRELATION_DBTYPE = type;
                            list.Add(Entity);
                        }
                        if (list != null && list.Count > 0)
                        {
                            if (_HTDWFRelagionBLL.AddHTDWFRelagion(SegmentID, list))
                            {
                                OutMessage = "保存成功！";
                                OutStatus = "200";
                            }
                        }
                    }
                }
                else
                {
                    if (_HTDWFRelagionBLL.DeleteHTDWFRelagionBySegmenID(SegmentID, type))
                    {
                        OutMessage = "保存成功！";
                        OutStatus = "200";
                    }
                }
            }
            return Json(new KingRoad.EPM.Web.ViewModels.CallbackStatus
            {
                message = OutMessage,
                statusCode = OutStatus,
                callbackType = "Create"
            });
        }
        /// <summary>
        /// 根据标段ID获取已存在数据
        /// </summary>
        /// <param name="SegmentID"></param>
        /// <param name="StaffID"></param>
        /// <param name="DutyID"></param>
        /// <returns></returns>
        public ActionResult CheckIsBeBySegmentIDAndStaffIDAndDutyID(int SegmentID, int type)
        {
            string OutMessage = string.Empty;
            string OutStatus = "300";
            List<PROJECT_HTDWFRELATION> list = null;
            if (SegmentID > 0)
            {
                list = _HTDWFRelagionBLL.GetlistBySegmentID(SegmentID, type);
            }
            return Json(new KingRoad.EPM.Web.ViewModels.CallbackStatus
            {
                message = OutMessage,
                statusCode = OutStatus,
                data = list,
                callbackType = "CheckIsBe"
            });
        }
    }
    class HTType
    {
        public string Flag { get; set; }
        public string Name { get; set; }
    }
}
